/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontera;

import Control.CrudCliente;
import Control.CrudCompra;
import Control.CrudTarea;
import Control.VerificarEstado;
import Entidad.Cliente;
import Entidad.Tarea;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.text.DateFormat;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author Stiv
 */
public class ClienteInf extends javax.swing.JFrame {
    
    public static DefaultTableModel dtmcompras;
    Color customColor = new Color(192,230,43);
    double id;
    
    
    public ClienteInf() {
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    public ClienteInf(double id) {
        
        initComponents();
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.id = id;
        inicializar();
    }
    
    

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NombreTF = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        idL = new javax.swing.JLabel();
        idTF = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        direccionL = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        telefonoL = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        emailL = new javax.swing.JLabel();
        cerrarBT = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        TablaCompras = new javax.swing.JTable();
        jLabel13 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(660, 383));

        NombreTF.setBackground(new java.awt.Color(204, 51, 0));
        NombreTF.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        NombreTF.setForeground(new java.awt.Color(255, 255, 255));
        NombreTF.setText("Nombre Cliente");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("ID");

        idL.setText("jLabel2");

        idTF.setBackground(new java.awt.Color(204, 51, 0));
        idTF.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        idTF.setForeground(new java.awt.Color(204, 204, 204));
        idTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                idTFActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Direccion");

        direccionL.setText("jLabel3");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Telefono");

        telefonoL.setText("jLabel7");

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Email");

        emailL.setText("jLabel5");

        cerrarBT.setText("Cerrar");
        cerrarBT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cerrarBTActionPerformed(evt);
            }
        });

        TablaCompras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablaCompras.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TablaComprasKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(TablaCompras);

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel13.setText("COMPRAS REALIZADAS");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(36, 36, 36)
                                                .addComponent(jLabel1)
                                                .addGap(38, 38, 38)
                                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(idL)
                                                    .addComponent(direccionL)))
                                            .addComponent(jLabel2))
                                        .addGap(131, 131, 131)
                                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(42, 42, 42)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel3))
                                        .addGap(50, 50, 50)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(telefonoL)
                                            .addComponent(emailL)))
                                    .addComponent(NombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, 575, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(cerrarBT)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 565, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel13)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(NombreTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(idL, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(direccionL))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jSeparator1)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(telefonoL)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(emailL)
                            .addComponent(jLabel3))))
                .addGap(21, 21, 21)
                .addComponent(jLabel13)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cerrarBT)
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void idTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_idTFActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_idTFActionPerformed

    private void cerrarBTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cerrarBTActionPerformed
        this.dispose ();
        this.setVisible (false);
        
    }//GEN-LAST:event_cerrarBTActionPerformed

    private void TablaComprasKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TablaComprasKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyChar() == KeyEvent.VK_ENTER){
        try{
            int fila = TablaCompras.getSelectedRow();
            int idcompra = 0;
              try{   
                  idcompra = (int)dtmcompras.getValueAt(fila,0);
              }
              catch(Exception g){
              }
              
              if(idcompra != 0){
                  CompraInf ventana = new CompraInf(idcompra);
                  ventana.setVisible(true);
              }
              else JOptionPane.showMessageDialog(TablaCompras, "Seleccione Tarea");
              evt.consume();
        }
        catch(Exception e){
            System.out.print("exception");
            e.printStackTrace();
            
        }
        
    }  
    }//GEN-LAST:event_TablaComprasKeyPressed

    
    
    /**
     * aram args the command line arguments
     
    public static void main(String args[]) {
        
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new VentanaInf().setVisible(true);
            }
        });
    }
    */
   public void inicializar(){
       
       Cliente c = CrudCliente.consultarCliente(id);
       NombreTF.setText(c.getNombre());
       idL.setText(Double.toString(c.getID()));
       direccionL.setText(c.getDireccion());
       telefonoL.setText(Integer.toString(c.getTelefono()));
       emailL.setText(c.getEmail());
       
        
        dtmcompras = new DefaultTableModel(new Object[][]{}, new String[]{"ID", "Valor", "fecha" }){};
        
        
        
        TablaCompras.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        TableColumnModel tsm = TablaCompras.getColumnModel();
        DiseñoColumna DC = new DiseñoColumna(Color.orange);
       
       TablaCompras.setModel(dtmcompras);
       
       for(Entidad.Compra p: CrudCompra.bringAll()){
           if (p.getCliente().getID()==id){
           dtmcompras.addRow(new Object[]{p.getID(), p.getValor(), p.getFecha()});
           }
       }
       TablaCompras.setDefaultRenderer(Object.class, new FormatoTabla());
       tsm.getColumn(0).setCellRenderer(DC);
    }
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField NombreTF;
    private javax.swing.JTable TablaCompras;
    private javax.swing.JButton cerrarBT;
    private javax.swing.JLabel direccionL;
    private javax.swing.JLabel emailL;
    private javax.swing.JLabel idL;
    private javax.swing.JTextField idTF;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel telefonoL;
    // End of variables declaration//GEN-END:variables
}
